﻿using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;
using HRMS_Model;
using HRMS.Models;
using System.Web.UI;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Lib.Web.Mvc.JQuery.JqGrid;


namespace HRMS.Controllers
{
    public class ApplicantController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Applicants(JqGridRequest request)
        {
            //get the count of roles
            var totalRecords = ApplicantsModels.GetApplicantsCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get Designations
            var applicants = ApplicantsModels.GetApplicants(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var applicant in applicants)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(applicant.ApplicantId), new List<object>() { 
                                    applicant.ApplicantId,
                applicant.FirstName,
                applicant.MiddleName,
                applicant.LastName,
                applicant.Address,                
                applicant.City,
                applicant.State,
                applicant.Zip,
                applicant.MobileNo,
                applicant.HomePhone,
                applicant.Email,
                applicant.Gender,
                applicant.DateOfBirth,
                applicant.MartialStatus,
                applicant.Degree.DegreeName,
                applicant.Skill.SkillName,
                applicant.Experience,
                applicant.ResumeSize,
                applicant.Resume
                }));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertApplicant([ModelBinder(typeof(CustomModelBinder))]Applicant applicantModel)
        {
            return Json(ApplicantsModels.AddApplicant(applicantModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateApplicant([ModelBinder(typeof(CustomModelBinder))]Applicant applicantModel)
        {
            return Json(ApplicantsModels.UpdateApplicant(applicantModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteApplicant(int id)
        {
            return Json(ApplicantsModels.DeleteApplicant(id));
        }

        public ActionResult GetDegrees()
        {
            return Json(DegreesModels.DegreesList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetSkills()
        {
            return Json(SkillsModels.SkillsList(), JsonRequestBehavior.AllowGet);
        }
    }
}
